package top.infopub.mgr.mgc.web;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mgc.service.PartnerInfoService;
import top.infopub.model.mgc.PartnerInfo;

import com.github.pagehelper.Page;


/**
 * 消息供应商管理控制层
 * @author Awoke
 * @version 2018年11月8日
 * @see MsgPartnerController
 * @since
 */
@Controller
@RequestMapping("/msgPartner")
public class MsgPartnerController extends GenericController {

    @Autowired
    private PartnerInfoService partnerInfoService;

    /** DEL_FLAG  0：可用  1：删除（不可用） **/
    /**可用*/
    private static final Integer DEL_ENABLE = 0;

    /**已删除*/
    private static final Integer DEL_DISABLE = 1;

    /**
     * 进入消息供应商管理
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "mgc/laymsg-partner-list";
    }

    /**
     * 查询消息供应商节点
     * @return
     */
    @RequestMapping("/query")
    public @ResponseBody RspVo<PartnerInfo> query(PartnerInfo bean, ParamPage paramPage) {
        Page<PartnerInfo> page = new Page<PartnerInfo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        bean.setDelFlag(DEL_ENABLE);
        Page<PartnerInfo> pageList = partnerInfoService.queryPage(page, bean);
        return RspVo.getSuccessResponse(pageList.getTotal(), pageList.getResult());
    }

    /**
     * 批量删除消息供应商
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete")
    public @ResponseBody RspVo delete(String[] checkboxId) {
        partnerInfoService.batchUpdateDelFlag(checkboxId, DEL_DISABLE);
        return RspVo.getSuccessResponse();
    }

    /**
     * 新增消息供应商
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/insert")
    public @ResponseBody RspVo insert(PartnerInfo bean) {
        if (partnerInfoService.isCodeExist(bean)) {
            return ErrorCodeEnum.ERROR_COMMON_CODE_UK.getRspVo();
        }
        bean.setDelFlag(DEL_ENABLE);
        bean.setIsDisable(DEL_ENABLE);
        bean.setAddTime(new Date());
        partnerInfoService.insert(bean);
        return RspVo.getSuccessResponse();
    }

    /**
     * 修改消息供应商
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/update")
    public @ResponseBody RspVo update(PartnerInfo bean) {
        PartnerInfo info = partnerInfoService.selectByPrimaryKey(bean.getId());
        if (info != null) {
            if (!info.getSpartnerCode().equals(bean.getSpartnerCode())) {
                if (partnerInfoService.isCodeExist(bean)) {
                    return ErrorCodeEnum.ERROR_COMMON_CODE_UK.getRspVo();
                }
            }
        }
        bean.setUpdateTime(new Date());
        partnerInfoService.updateBySelective(bean);
        return RspVo.getSuccessResponse();
    }

}
